package com.isoftstone.service.impl;

import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isoftstone.mapper.UserMapper;
import com.isoftstone.pojo.Users;
import com.isoftstone.service.UserService;
import com.isoftstone.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

//业务逻辑接口实现类

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public Result<Users> queryUserByPhone(String phone) {
        return null;
    }
    @Override
    public Result<?> insertUser(Users user) {
        Users users = userMapper.queryUserByPhone(user.getPhone());
        if (users == null) {
            //系统生成时间
            user.setCreateData(new Date());
            //将密码进行加密存储

            user.setPassword(new String(SecureUtil.md5(user.getPassword())));
            int insert = userMapper.insertUser(user);
            if (insert > 0) {
                return Result.success("注册成功");
            }
            return Result.error("注册失败");

        }
        else {
            return Result.error("该手机号已注册");
        }

    }
    @Override
    public Result<?> updateUser(Users user) {
        int update = userMapper.updateUser(user);
        if (update > 0) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }


    //删除用户信息
    @Override
    public Result<?> deleteUser(Integer id) {
        int result = userMapper.deleteUser(id);
        if (result > 0) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @Override
    public Page queryAllUser(Integer currentSize, Integer pageSize) {
        Page<Users> page = PageHelper.startPage(currentSize, pageSize);
        userMapper.queryAllUser();
        return page;
    }

    @Override
    public Result<?> login(String phone, String password , String photo) {
        Users users = userMapper.login(phone, new String(SecureUtil.md5(password)), photo);
        return users == null ? Result.error("账号或密码错误") : Result.success(users);
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws IOException {
        //查询所有用户信息
        List<Users> users = userMapper.queryAllUser();
        //通过工具类生成excel
        ExcelWriter writer = ExcelUtil.getWriter();
        //将查询结果写入Excel
        writer.write(users,true);
        //设置浏览器响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("用户信息","UTF-8");
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename="+fileName+".xlsx");
        //获取输出流对象
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        //关闭流对象
        writer.close();
        IoUtil.close(out);
    }

    @Override
    public Result<?> deleteBatchUser(List<Integer> idList) {
        try {
            int count = userMapper.deleteBatchUser(idList);
            if (count > 0) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("批量删除发生异常");
        }
    }


}
